package com.runa.rccprotocol.dto.runa;

import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.ConcentratorRequestData;

public class RunaRCRequestData extends ConcentratorRequestData {

    private byte[] requestData;

    public RunaRCRequestData() {
        super(null);
    }

    public static void main(String[] args) {
        String s = "21801638";
        RunaRCRequestData requestData = new RunaRCRequestData();
        requestData.readHeatMeter(s);
        System.out.println(ByteUtil.byteToHexString(requestData.requestData));

    }

    public void readHeatMeter(String meter) {
        requestData = new byte[20];
        requestData[0] = (byte) 0xFE;
        requestData[1] = (byte) 0xFE;
        requestData[2] = (byte) 0xFE;
        requestData[3] = (byte) 0xFE;
        requestData[4] = 0x68;
        requestData[5] = 0x20;
        requestData[6] = (byte) Integer.parseInt(meter.substring(6, 8), 16);
        requestData[7] = (byte) Integer.parseInt(meter.substring(4, 6), 16);
        requestData[8] = (byte) Integer.parseInt(meter.substring(2, 4), 16);
        requestData[9] = (byte) Integer.parseInt(meter.substring(0, 2), 16);
        requestData[10] = 0;
        requestData[11] = 0x11;
        requestData[12] = 0x11;
        requestData[13] = 1;
        requestData[14] = 3;
        requestData[15] = 0x1f;
        requestData[16] = (byte) 0x90;
        requestData[17] = 0x00;
        requestData[18] = ByteUtil.sum(requestData, 4, 18);
        requestData[19] = 0x16;
        setHaveResult(true);
    }

    public void readHouseValve(String valve) {
        requestData = new byte[16];
        requestData[0] = 0x68;
        requestData[1] = (byte) 0x81;
        requestData[2] = (byte) Integer.parseInt(valve.substring(6, 8), 16);
        requestData[3] = (byte) Integer.parseInt(valve.substring(4, 6), 16);
        requestData[4] = (byte) Integer.parseInt(valve.substring(2, 4), 16);
        requestData[5] = (byte) Integer.parseInt(valve.substring(0, 2), 16);
        requestData[6] = 0;
        requestData[7] = 0x11;
        requestData[8] = 0x11;
        requestData[9] = 1;
        requestData[10] = 3;
        requestData[11] = 0x1f;
        requestData[12] = (byte) 0x90;
        requestData[13] = 0x12;
        requestData[14] = ByteUtil.sum(requestData, 0, 14);
        requestData[15] = 0x16;
        setHaveResult(true);
    }

    public void readBuildValve(String valve) {
        requestData = new byte[16];
        requestData[0] = 0x68;
        requestData[1] = 0x71;
        requestData[2] = (byte) Integer.parseInt(valve.substring(6, 8), 16);
        requestData[3] = (byte) Integer.parseInt(valve.substring(4, 6), 16);
        requestData[4] = (byte) Integer.parseInt(valve.substring(2, 4), 16);
        requestData[5] = (byte) Integer.parseInt(valve.substring(0, 2), 16);
        requestData[6] = 0;
        requestData[7] = 0x11;
        requestData[8] = 0x11;
        requestData[9] = 1;
        requestData[10] = 3;
        requestData[11] = 0x1f;
        requestData[12] = (byte) 0x90;
        requestData[13] = 0;
        requestData[14] = ByteUtil.sum(requestData, 0, 14);
        requestData[15] = 0x16;
        setHaveResult(true);
    }

    public void setBuildValveOpen(String valve, int open) {
        requestData = new byte[20];
        requestData[0] = 0x68;
        requestData[1] = 0x71;
        requestData[2] = (byte) Integer.parseInt(valve.substring(6, 8), 16);
        requestData[3] = (byte) Integer.parseInt(valve.substring(4, 6), 16);
        requestData[4] = (byte) Integer.parseInt(valve.substring(2, 4), 16);
        requestData[5] = (byte) Integer.parseInt(valve.substring(0, 2), 16);
        requestData[6] = 0;
        requestData[7] = 0x11;
        requestData[8] = 0x11;
        requestData[9] = 4;
        requestData[10] = 7;
        requestData[11] = 0x1f;
        requestData[12] = (byte) 0x90;
        requestData[14] = 1;
        requestData[15] = 2;
        requestData[16] = (byte) (open >> 8);
        requestData[17] = (byte) open;
        requestData[18] = ByteUtil.sum(requestData, 0, 18);
        requestData[19] = 0x16;
        setHaveResult(true);
    }

    public void setBuildValveModel(String valve, boolean model) {
        requestData = new byte[20];
        requestData[0] = 0x68;
        requestData[1] = 0x71;
        requestData[2] = (byte) Integer.parseInt(valve.substring(6, 8), 16);
        requestData[3] = (byte) Integer.parseInt(valve.substring(4, 6), 16);
        requestData[4] = (byte) Integer.parseInt(valve.substring(2, 4), 16);
        requestData[5] = (byte) Integer.parseInt(valve.substring(0, 2), 16);
        requestData[6] = 0;
        requestData[7] = 0x11;
        requestData[8] = 0x11;
        requestData[9] = 4;
        requestData[10] = 7;
        requestData[11] = 0x1f;
        requestData[12] = (byte) 0x90;
        requestData[14] = 3;
        requestData[15] = 2;
        requestData[17] = (byte) (model ? 1 : 0);
        requestData[18] = ByteUtil.sum(requestData, 0, 18);
        requestData[19] = 0x16;
        setHaveResult(true);
    }

    @Override
    public boolean check(IResponseData data) {
        return true;
    }

    @Override
    public byte[] getData() {
        return requestData;
    }
}
